package com.iworkh.spring.web.mvc.controller;

import com.iworkh.spring.web.mvc.domain.vo.Site;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * ModelDataController
 *
 * @author: iworkh-沐雨云楼
 * @date: 2019-12-27
 */

@Controller
@RequestMapping("/springmvc")
public class ModelDataController {

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {

        System.out.println("iworkh--testModelAndView");

        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "iworkh-沐雨云楼");
        dataMap.put("url", "https://www.iworkh.com");

        modelAndView.addAllObjects(dataMap);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/testModel")
    public String testModel(Model model) {

        System.out.println("iworkh--testModel");

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "iworkh-沐雨云楼");
        dataMap.put("url", "https://www.iworkh.com");
        model.addAllAttributes(dataMap);

        return "success";
    }

    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap) {

        System.out.println("iworkh--testModelMap");

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "iworkh-沐雨云楼");
        dataMap.put("url", "https://www.iworkh.com");
        modelMap.addAllAttributes(dataMap);

        return "success";
    }

    @RequestMapping("/testMap")
    public String testMap(Map<String, Site> map) {
        System.out.println("iworkh--testMap");
        Site site = Site.builder().name("iworkh-沐雨云楼").url("https://www.iworkh.com").build();
        map.put("site", site);
        return "success";
    }

}
